﻿using LitJson;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace FrameWorkSong
{
    //   public class AudioManager : MonoBehaviourSimplify
    public class AudioManager : Singleton<AudioManager>
    {
        //public static AudioManager _instance;
        //public static AudioManager Instance
        //{
        //    get
        //    {
        //        if (null == _instance)
        //        {
        //            _instance = new GameObject("AudioManager").AddComponent<AudioManager>();
        //        }
        //        return _instance;
        //    }
        //}

        private Dictionary<AudioName, string> audioPathDict;//存储音频路径
        Stack<AudioSource> AudioSourcePool = new Stack<AudioSource>();//AudioSource的对象池
        List<AudioSource> audioSources = new List<AudioSource>();//AudioSource的储存
        ResLoader resLoader = new ResLoader();//资源管理器
        /// <summary>
        /// 播放声音
        /// </summary>
        /// <param name="audioName"></param>
        public void PlaySound(AudioName audioName)
        {
            if (audioPathDict == null)
            {
                ParseAudioNameJson();
            }
            var coinClip = resLoader.LoadAssetSync<AudioClip>(audioName.ToString(),audioPathDict[audioName]);
        
            AudioSource audioSource;
            if (AudioSourcePool.Count > 0)
            {
                audioSource = AudioSourcePool.Pop();
            }
            else
            {
                audioSource = gameObject.AddComponent<AudioSource>();
                audioSources.Add(audioSource);
            }
            audioSource.clip = coinClip;
            audioSource.Play();
            Delay(coinClip.length, () => {
              
                audioSource.clip = null;
                AudioSourcePool.Push(audioSource);
                resLoader.Release(coinClip.name);


            });
        }
   
        /// <summary>
        /// 打开声音
        /// </summary>
        public void OffSound()
        {
            foreach (var item in audioSources)
            {
                item.Pause();
                item.mute = true;
            }
        }
        /// <summary>
        /// 关闭声音
        /// </summary>
        public void OnSound()
        {
            foreach (var item in audioSources)
            {
                item.UnPause();
                item.mute = false;
            }
        }

        AudioName nowAudio= AudioName.Null;
        private AudioSource mianAudioSound;
        /// <summary>
        /// 播放音乐
        /// </summary>
        /// <param name="audioName"></param>
        /// <param name="loop"></param>
        public void PlayMusic(AudioName audioName, bool loop)
        {
            nowAudio = audioName;
            if (audioPathDict == null)
            {
                ParseAudioNameJson();
            }
            if (!mianAudioSound)
            {
                mianAudioSound = gameObject.AddComponent<AudioSource>();
            }

            var coinClip = resLoader.LoadAssetSync<AudioClip>(audioName.ToString(), audioPathDict[audioName]);
            mianAudioSound.clip = coinClip;
            mianAudioSound.loop = loop;
            mianAudioSound.Play();
        }
        /// <summary>
        /// 播放音乐
        /// </summary>
        /// <param name="audioName"></param>
        public void PlayMusic(AudioName audioName)
        {
            nowAudio = audioName;
            if (audioPathDict == null)
            {
                ParseAudioNameJson();
            }
            if (!mianAudioSound)
            {
                mianAudioSound = gameObject.AddComponent<AudioSource>();
            }
            AudioClip asste = mianAudioSound.clip;
            if (asste!=null)
            {
                resLoader.Release(asste.name);
            }
            PlayMusic(audioName, true);
        }
        /// <summary>
        /// 停止音乐
        /// </summary>
        public void StopMusic()
        {
            if (!mianAudioSound) return;
            mianAudioSound.Stop();
        }
        /// <summary>
        /// 暂停音乐
        /// </summary>
        public void PauseMusic()
        {
            if (!mianAudioSound) return;
            mianAudioSound.Pause();
        }
        /// <summary>
        /// 继续音乐
        /// </summary>
        public void ResumeMusic()
        {
            if (!mianAudioSound) return;
            mianAudioSound.UnPause();
        }
        /// <summary>
        /// 打开音乐
        /// </summary>
        public void OffMusic()
        {
            if (!mianAudioSound) return;
            mianAudioSound.Pause();
            mianAudioSound.mute = true;
        }
        /// <summary>
        /// 关闭音乐
        /// </summary>
        public void OnMusic()
        {
            if (!mianAudioSound) return;
            mianAudioSound.UnPause();
            mianAudioSound.mute = false;
        }


        /// <summary>
        /// 解析音频信息
        /// </summary>
        private void ParseAudioNameJson()
        {
            if (audioPathDict == null)
            {
                audioPathDict = new Dictionary<AudioName, string>();
            }
            TextAsset jsonText = Resources.Load<TextAsset>("Config/AudioNameJson");
            JsonData jsonData = JsonMapper.ToObject(jsonText.text);
            int length = jsonData.Count;
            for (int i = 0; i < length; i++)
            {
                AudioInfo audioInfo = new AudioInfo();
                audioInfo.AudioInfoString = jsonData[i]["audioName"].ToString();
                audioInfo.Path = jsonData[i]["path"].ToString();
                audioPathDict.Add(audioInfo.audioName, audioInfo.Path);
            }
        }
        /// <summary>
        /// 卸载指定声音
        /// </summary>
        /// <param name="audioName"></param>
        public void UnloadMusic(AudioName audioName)
        {

            resLoader.Release(audioPathDict[audioName]);
        }
        /// <summary>
        /// 卸载声音
        /// </summary>
        public void UnloadMusic()
        {
            resLoader.ReleaseAll();
        }

        public override void OnBeforeDestroy()
        {
            resLoader.ReleaseAllRecord();
        }
    }

    [Serializable]
    public class AudioInfo
    {

        public AudioName audioName;
        private string path;
        public string AudioInfoString
        {
            get
            {
                return audioName.ToString();
            }
            set
            {
                audioName = (AudioName)Enum.Parse(typeof(AudioName), value);
            }
        }


        public string Path
        {
            get
            {
                return path;
            }

            set
            {
                path = value;
            }
        }
    }
}
